package com.hqyj.lx.controller;

import com.hqyj.lx.pojo.Exampaper;
import com.hqyj.lx.pojo.Student;
import com.hqyj.lx.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class StudentController {

    @Autowired
    private StudentService studentService;
    //全查学生功能
    @RequestMapping("/queryAllStudent")
    public String queryAllStudent(ModelMap modelMap){
        List<Student> students = studentService.queryAllStudents();
        modelMap.put("students",students);
        return "studentList";
    }
    //增加学生功能
    @RequestMapping(path = "/insertStudent",method = {RequestMethod.POST})
    public String insertStudent(Student student){
        int i = studentService.insertOneStudent(student);
        if(i==1){
            return "redirect:queryAllStudent";
        }
        return "studentError";
    }
    //删除学生功能
    @RequestMapping("/deleteStudent")
    public String deleteStudent(int sId){
        int i = studentService.deleteOneStudent(sId);
        if(i==1){
            return "redirect:queryAllStudent";
        }
        return "studentError";
    }
    //更新学生信息
    @RequestMapping("/updateStudent")
    public String updateStudent(Student student){
        System.out.println(student);
        int i = studentService.updateStudent(student);
        if(i==1){
            return "redirect:queryAllStudent";
        }
        return "studentError";
    }
    //通过id查询数据
    @RequestMapping("/queryStudentById")
    public String queryStudentById(int sId,ModelMap modelMap) {
        Student student = studentService.queryStudentById(sId);
        System.out.println(student);
        modelMap.put("student", student);
        return "updateStudent";
    }

    //修改密码
    //通过id查询数据
    @RequestMapping("/queryStudentByIdToUpdateSpwd")
    public String queryStudentByIdToUpdateSpwd(ModelMap modelMap,HttpSession session) {
        int sId=(int)session.getAttribute("sId");
        Student student = studentService.queryStudentById(sId);
        System.out.println(student);
        modelMap.put("student", student);
        return "queryOneStudent";
    }
    //通过id查询数据
    @RequestMapping("/queryOneStudentById")
    public String queryOneStudentById(int sId,ModelMap modelMap) {
        Student student = studentService.queryStudentById(sId);
        System.out.println(student);
        modelMap.put("student", student);
        return "updateStudentPwd";
    }

    @RequestMapping("/kaoSheng")
    public String kaoSheng(Student student){
        System.out.println(student);
        int i = studentService.updateStudent(student);
        if(i==1){
            return "redirect:/kaoShengIndex.jsp";
        }
        return "studentError";
    }

    @RequestMapping("studentLogout")
    public String studentLogout(HttpSession session){
        //清除session共享的数据
        session.removeAttribute("student");
        session.removeAttribute("sId");
        session.removeAttribute("sName");
        session.removeAttribute("sPassword");
        //重定向到登录界面
        return "redirect:/login.jsp";
    }

}
